﻿using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.AdvTree;

namespace MachineBuildingSystem
{
    public partial class ActionMgr : XtraForm
    {
        public ActionMgr()
        {
            InitializeComponent();
        }

        private void ActionMgr_Load(object sender, EventArgs e)
        {
            RefreshActionTree();
        }
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            RefreshActionTree();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            List<string> list = new List<string> ();
            foreach (Node node in advTreeAction.Nodes)
                list.Add(node.Text);
            ActionForm form = new ActionForm();
            form.IsAdd = true;
            form.NameList = list;
            if (DialogResult.OK == form.ShowDialog())
            {
                string name = form.Name;
                string sql = "insert into HS_ACTION (name_) values ('" + name + "')";
                try
                {
                    bool isSuccess = DB2Helper.CreateInstance().DoDB2Execute(new string[] { sql });
                    if (!isSuccess)
                    {
                        MessageBox.Show(this, "插入操作名称失败！");
                        return;
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.error("插入操作名称异常：" + ex.Message);
                    MessageBox.Show(this, "插入操作名称异常：" + ex.Message);
                    return;
                }
                MessageBox.Show(this, "新增名称成功！");
                RefreshActionTree();
            }
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            List<string> list = new List<string>();
            foreach (Node node in advTreeAction.Nodes)
                list.Add(node.Text);
            Node selectedNode = advTreeAction.SelectedNode;
            if (selectedNode == null)
            {
                MessageBox.Show(this, "请选择需要删除的名称");
                return;
            }
            long id = long.Parse(selectedNode.Tag.ToString());
            ActionForm form = new ActionForm();
            form.Name = selectedNode.Text;
            form.IsAdd = false;
            form.NameList = list;
            if (DialogResult.OK == form.ShowDialog())
            {
                string name = form.NewName;
                string sql = "update HS_ACTION set name_ = '" + name + "' where id = " + id;
                try
                {
                    bool isSuccess = DB2Helper.CreateInstance().DoDB2Execute(new string[] { sql });
                    if (!isSuccess)
                    {
                        MessageBox.Show(this, "修改操作名称失败！");
                        return;
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.error("修改操作名称异常：" + ex.Message);
                    MessageBox.Show(this, "修改操作名称异常：" + ex.Message);
                    return;
                }
                MessageBox.Show(this, "修改名称成功！");
                RefreshActionTree();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            Node node = advTreeAction.SelectedNode;
            if(node == null)
            {
                MessageBox.Show(this, "请选择需要删除的名称");
                return;
            }
            long id = long.Parse(node.Tag.ToString());
            string sql = "delete from HS_ACTION where id =" + id;
            try
            {
                bool isSuccess = DB2Helper.CreateInstance().DoDB2Execute(new string[]{sql});
                if (!isSuccess)
                {
                    MessageBox.Show(this, "删除名称失败！");
                    return;
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("删除操作名称异常：" + ex.Message);
                MessageBox.Show(this, "删除操作名称异常：" + ex.Message);
                return;
            }
            MessageBox.Show(this, "删除名称成功！");
            RefreshActionTree();
        }

        private void RefreshActionTree()
        {
            DataTable dt = RefreshActionData();
            if (dt == null || dt.Rows.Count == 0)
                return;
            advTreeAction.Nodes.Clear();
            try
            {
                foreach (DataRow dr in dt.Rows)
                {
                    Node node = new Node();
                    node.Text = dr["name_"].ToString();
                    node.Tag = long.Parse(dr["id"].ToString());
                    advTreeAction.Nodes.Add(node);
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("加载操作树节点异常：" + ex.Message);
                MessageBox.Show("加载操作树节点异常：" + ex.Message);
            }
        }

        //刷新列表操作
        private DataTable RefreshActionData()
        {
            DataTable dt = new DataTable();
            try
            {
                string sql = "select * from HS_ACTION order by ID";
                dt = DB2Helper.CreateInstance().DoDB2Select(sql);
            }
            catch (Exception ex)
            {
                LogHelper.error("刷新操作功能信息异常：" + ex.Message);
                MessageBox.Show("刷新操作功能信息异常：" + ex.Message);
                return null;
            }
            return dt;
        }
    }
}
